/*
 *  AMPListBoxSelectionObserver.cpp
 *  AMPlugin
 *
 *  Created by Vu Tiet on 6/14/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */


//===========================
//  Plug-in includes
//===========================

#include "VCPlugInHeaders.h"

// Interface includes
#include "IControlView.h"

// General includes
#include "SelectionObserver.h"
#include "ILayoutUIUtils.h"
#include "ISelectionUtils.h"

// Project includes
#include "AMPID.h"
#include "ISnipRunSuite.h"
#include "AMGlobal.h"

#include <iostream>
using namespace std;
#include "IFrmLblData.h"
#include "AMPLogDataRecord.h"
extern vector<pt_assignStr> g_assignmentPtr;
/** Enables or disables the button when the selection changes dependent
 on whether ISnipRunSuite::CanRun indicates the snippet can or cannot 
 be run.
  
 This observer subclasses ActiveSelectionObserver so when there is a selection change
 SnipRunGoSelectionObserver will get notified.  
 
 */
class AMPListBoxSelectionObserver : public ActiveSelectionObserver
	{
	public:
		
		/**
		 Constructor.
		 @param boss interface ptr from boss object on which this interface is aggregated.
		 */
		AMPListBoxSelectionObserver(IPMUnknown* boss);
		
		/**
		 Destructor.
		 */
		virtual ~AMPListBoxSelectionObserver();
		
	protected:
		
		/** Called when the selection changes.
		 */
		virtual void HandleSelectionChanged(const ISelectionMessage* message);
	};

/* Make the implementation available to the application.
 */
CREATE_PMINTERFACE(AMPListBoxSelectionObserver, kAMPListBoxSelectionObserverImpl)


/*
 */
AMPListBoxSelectionObserver::AMPListBoxSelectionObserver(IPMUnknown* boss) :
ActiveSelectionObserver(boss, IID_SELECTIONOBSERVER)
{
	//printf("AMPListBoxSelectionObserver created \n");
}

/*
 */
AMPListBoxSelectionObserver::~AMPListBoxSelectionObserver()
{
}

/* 
 */
void AMPListBoxSelectionObserver::HandleSelectionChanged(const ISelectionMessage* message)
{
	//printf("AMPListBoxSelectionObserver::HandleSelectionChanged \n");
	
	do {
		if (g_IsLoggedIn != TRUE) {
			break;
		}
		
		InterfacePtr<ISnipRunSuite> suite(Utils<ISelectionUtils>()->GetActiveSelection(), UseDefaultIID());
		if (!suite) {
			// If there's no ISnipRunSuite then no snippets can be run so disable the button.
			//controlView->Enable(kFalse);
			printf("there is no ISnipRunSuite \n");
			break;
		}
		// Get the name of the snippet to work with.
		ISnipRunSuite::SnippetName _snippetName = PMString("HandleGraphicFrame");
		
		// Enable or disable the button according to whether or not the rnippet can run.
		bool16 _canRun = suite->CanRun(_snippetName);
		ISnipRunSuite::SnippetName snippetName = PMString("CheckInStory");
		
		// Enable or disable the button according to whether or not the rnippet can run.
		bool16 canRun = suite->CanRun(snippetName);
		//cout << "canrun - g_PrevTextSelected - g_TextSelected: " << canRun << " - " << g_PrevTextSelected << " - " << g_TextSelected << endl;
		if (canRun == FALSE && g_TextSelected == TRUE) { // the story has not been checked out and user selected text frame
			// check out the story silently
			ErrorCode status = kFailure;
			ISnipRunSuite::SnippetName checkOutSnippetName = PMString("CheckOutStory");
			if (suite->CanRun(checkOutSnippetName)) {
				PMString parameters("");
				bool16 enableParameterDialog = kFalse;
				status = suite->Run(checkOutSnippetName, parameters, enableParameterDialog);
			}
			
			if (status == kFailure) {
				// deselect text frame
				InterfacePtr<ISelectionManager> selectionManager(Utils<ISelectionUtils>()->QueryActiveSelection());
				selectionManager->DeselectAll(nil);
				g_TextSelected = FALSE;
			}
			else {
				//<--
				AMPUtils::SaveDocument();	//Toan added here
				//-->
				SaveLogHeaderFile();
			}
			
		}
		else if(g_PrevTextSelected == TRUE && g_TextSelected == FALSE) { // user clicks outside text frame after entered it
			bool shouldShowAlert = FALSE;
			for (int i = 0; i < g_TextFrameCount; i++) {
				if (g_SelectedTextFrameUID.compare(g_TextFrames[i].textframeUID) == 0 && g_TextFrames[i].isLocked == TRUE) {
					shouldShowAlert = TRUE;
					break;
				}
			}
			if (shouldShowAlert == TRUE) {
			}
		}
		
		if (g_PrevTextSelected != g_TextSelected) {
			//if (g_IsLoggedIn == TRUE && g_IsCheckOut == TRUE) {
			if (g_IsLoggedIn == TRUE && g_DocName.length() > 0) {
				AMPUtils::RefreshMainPanel(kAMPLuginCheckoutActionID);
			}
			else {
				AMPUtils::RefreshMainPanel(FALSE);
			}
		}
		
	} while(false);
}

// End, AMPListBoxSelectionObserver.cpp.
